﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ITAP.ContourBuilder.Components;
using System.Drawing;
using ITAP.Core.Modelling;


namespace ITAP.ContourBuilder.Manipulation
{
    public class CommandAddCommentLine : Command
    {
        public CommandAddCommentLine(ContourControl control)
            : base(control)
        {

        }
        public override void Execute()
        {
            base.Execute();
        }

        ContourCommentLine commentLine = null;
        PointF prevMousePoint;
        public override void ObtainPeripheryState(GlobalPeripheryState state)
        {
            base.ObtainPeripheryState(state);
            
            // Вводим первый конец линии
            if (state.MouseState.MouseMode == MouseMode.Down && state.MouseState.State.Button == System.Windows.Forms.MouseButtons.Left)
            {
                if (commentLine == null)
                {
                    ModelPoint startPoint = this.ScreenPointToModelPoint(state.MouseState.State.Location);
                    prevMousePoint = state.MouseState.State.Location;
                    commentLine = ContourElementFactory.CreateCommentLine(startPoint, startPoint);
                    this.control.ContourContainer.AddInnerComponent(commentLine);
                    this.control.ContourContainer.Update();
                    this.control.ToleranceManager.Visit(this.control.ContourContainer);
                }
                else
                {
                    this.control.ToleranceManager.Visit(this.control.ContourContainer);
                    this.control.ContourContainer.Update();
                    this.Exit();
                }
            }
            // Перемещаем второй конец линии
            else if (state.MouseState.MouseMode == MouseMode.Move && state.MouseState.State.Button == System.Windows.Forms.MouseButtons.None &&
                commentLine != null)
            {
                ModelPoint prevPoint = this.ScreenPointToModelPoint(prevMousePoint);
                ModelPoint point = this.ScreenPointToModelPoint(state.MouseState.State.Location);
                
                commentLine.MoveEndPoint(new ModelVector(prevPoint, point));
                prevMousePoint = state.MouseState.State.Location;
                this.control.ContourContainer.Update();
            }           
            
            this.control.Invalidate();
        }

        public override string Name
        {
            get
            {
                return "Добавить линию комментария";
            }
        }

        public override string ToString()
        {
            return "Добавить линию комментария";
        }

        
    }
}
